import serviceStatus from "../../enum/service-status";
import serviceType from "../../enum/service-type";
import Rating from "../../model/rating";
import Service from "../../model/service";
import User from "../../model/user";
import { getEventParam } from "../../utils/utils";

import serviceAction from "../../enum/service-action";
import cache from "../../enum/cache";


const rating = new Rating()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceId:0,
    serviceTypeEnum:serviceType,
    serviceStatusEnum:serviceStatus,
    service: {
      id:1,
      cover_image:{
        id: 24,
        path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
      },
      category:{
        id:6,
        name:"护理"
      },
      title:"北京市招java开发一名",
      price:"10000.00",
      publisher:{
        id:1,
        nickname:"刘唯卿",
        avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
      },
      create_time:"2024-10-14",
      sales_volume:0,
      score:4.5,
      description:"招java开发一名",
      status:1,
      type:1
    
    },
    isPublisher:false,
    ratingList:[
      {
        id:1,
        score:5,
        content:"技术很牛逼",
        create_time:"2024-10-01 10:00:00",
        author:{
          avater:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg",
          nickname:"刘唯卿"
        },
        illustration:["https://c-ssl.duitang.com/uploads/item/201909/15/20190915154013_gmukm.jpg","https://c-ssl.duitang.com/uploads/blog/202303/02/20230302123650_94ec1.jpg"]
      },
      {
        id:2,
        score:3,
        content:"技术很牛逼",
        create_time:"2024-10-01 10:00:00",
        author:{
          avater:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg",
          nickname:"刘唯卿"
        },
        illustration:["https://c-ssl.duitang.com/uploads/item/201909/15/20190915154013_gmukm.jpg","https://c-ssl.duitang.com/uploads/blog/202303/02/20230302123650_94ec1.jpg"]
      }
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
   onLoad(options) {
    
    this.data.serviceId = options.service_id
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {

    if(!rating.hasMoreData){
      return
    }

    const ratingList = await rating.getServiceRatingList(this.data.serviceId)
    this.setData({
      ratingList
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },


  // 更新操作
  async handleUpdateStatus(event){
  
    const action = getEventParam(event,'action')
  
    const content = this._generateModelContent(action)
    const res = await wx.showModal({
      title: '注意',
      content: content,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
    });

    if(!res.confirm){
      return
    }
    
      

  },
  //编辑操作
   handleEditService(event){
   
  
    const service = JSON.stringify(this.data.service)
    wx.navigateTo({
      url:`/pages/service-edit/service-edit?service=${service}`
    })

  },

  // 联系对方
  handleChat(){
    const targetUserId = this.data.service.publisher.id
    const service = JSON.stringify(this.data.service)

    wx.navigateTo({
      url:`/pages/conversation/conversation?targetUserId=${targetUserId}&service=${service}`
    })
  },

  handleOrder(){
    
    // 判断是否登录 
    if(!wx.getStorageSync(cache.TOKEN)){
      wx.navigateTo({
        url:'/pages/login/login',
        // 事件监听
        events:{
          // 箭头函数
          login:() => {
            this._checkRole()
          }
        }
      })
      return
    }

    const service = JSON.stringify(this.data.service)

    wx.navigateTo({
      url:`/pages/order/order?service=${service}`
    })


  },

  async _getService(){

    const service = await Service.getServiceById(this.data.serviceId)
    this.setData({
      service
    })
  },

  _checkRole(){
    const userInfo = User.getUserInfoByLocal();
    if(userInfo && userInfo.id === this.data.service.publisher.id){
      this.setData({
        isPublisher: true
      })
    }
  },
  _getServiceRatingList(){

    // 如果是正在找
    if(this.data.service.type === serviceType.SEEK){
      return
    }

    //const ratingList = await rating.reset().getServiceRatingList(this.data.serviceId)
      // this.setData({
      //   ratingList
      // })

  },

  _generateModelContent(action){
    let content

    switch(action){
      case serviceAction.PAUSE:
        content:'暂停后服务状态将变为“待发布”,' + '可在个人中心操作重新发布上线，' + '是否确认暂停发布该服务?'
        break;
        case serviceAction.PUBLISH:
          content = '发布后即可在广场页面中被浏览到，是否确认发布'
        break;
        case serviceAction.CANCEL:
          content = '取消后不可恢复，需要重新发布并提交审核；' + '已关联该服务的订单且订单状态正在进行中的，仍需正常履约;' + '是否确认取消该服务？'
        break;
    }


    return content


  }

  
})

